﻿Imports System.Data
Imports System.Data.OleDb
Imports System.Web.Configuration
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim ID As Integer
        'get tag ID from query string
        If Request.QueryString("ID") Is Nothing Then
            'no ID in query string so redirect to home page
            Response.Redirect("~/home.aspx")
        Else
            If Not Regex.IsMatch(Request.QueryString("ID"), "^[0-9]{1,}$") Then
                'ID in query string is not an integer so redirect to home page
                Response.Redirect("~/home.aspx")
            End If
            'set songID class member to song ID
            ID = CInt(Request.QueryString("ID"))
        End If

        'populate page with text from database stored in "homepage" table
        'declare and instantiate connection object - connection string is stored in web.config
        Dim myConnection = New OleDbConnection()
        myConnection.ConnectionString = WebConfigurationManager.ConnectionStrings("yobboGuitar").ConnectionString
        'declare and instantiate command object

        Dim myCommand = New OleDbCommand("SELECT songID, title FROM tagText WHERE ID=" & ID)
        Dim myReader As OleDbDataReader


        Try
            'open connection
            myConnection.Open()
            'set connection property of command object
            myCommand.Connection = myConnection
            'execute reader
            myReader = myCommand.ExecuteReader
            If myReader.HasRows Then
                'set data source property of grid view and invoke dataBind method to fill control
                Me.GridView_results.DataSource = myReader
                Me.GridView_results.DataBind()
            End If
            'close connection
            myConnection.Close()
        Catch ex As Exception
            'redirect to error message page
            Response.Redirect("~/error.htm")
        Finally
            myConnection.Close()
        End Try

        myCommand = New OleDbCommand("SELECT tagText FROM tags WHERE ID=" & ID)
        Dim tagText As String
        Try
            'open connection
            myConnection.Open()
            'set connection property of command object
            myCommand.Connection = myConnection
            'execute reader
            myReader = myCommand.ExecuteReader
            myReader.Read()
            If myReader.HasRows Then
                tagText = myReader("tagText")

                Me.heading_tagName.InnerText = "Songs tagged as '" & tagText & "'"
            End If
            'close connection
            myConnection.Close()
        Catch ex As Exception
            'redirect to error message page
            Response.Redirect("~/error.htm")
        Finally
            myConnection.Close()
        End Try
    End Sub
End Class
